<template>
  <div class="exam">
    <el-table :data="examInfo.arr" border v-loading="loading">
      <el-table-column fixed="left" prop="subject" label="试卷名称" width="180"></el-table-column>
      <el-table-column prop="description" label="描述" width="200"></el-table-column>
      <el-table-column prop="practiceOrExam" label="考试/练习" width="200"></el-table-column>
      <el-table-column prop="paperId" label="所属题库" :formatter="formatterPaper" width="200"></el-table-column>
      <el-table-column prop="institute" label="所属学院" width="120"></el-table-column>
      <el-table-column prop="major" label="所属专业" width="200"></el-table-column>
      <el-table-column prop="grade" label="考试年级" width="100"></el-table-column>
      <el-table-column prop="clazz" label="考试班级" width="100"></el-table-column>
      <el-table-column prop="examDate" label="考试日期" width="120"></el-table-column>
      <el-table-column prop="time" label="考试时长" width="120"></el-table-column>
      <el-table-column prop="examScore" label="试卷总分" width="120"></el-table-column>
      <el-table-column prop="type" label="试卷类型" width="120"></el-table-column>
      <el-table-column prop="tips" label="考生提示" width="400"></el-table-column>
      <el-table-column fixed="right" label="操作" width="150">
        <template #default="scope">
          <el-button @click="part(scope.row.id)" type="primary" size="small">查看分段</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-config-provider :locale="zhCn">
      <el-pagination
          @size-change="pageSizeChange"
          @current-change="pageNumChange"
          :current-page="pagination.pageNum"
          :page-sizes="[3, 6, 9, 12]"
          :page-size="pagination.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total">
      </el-pagination>
    </el-config-provider>
  </div>
</template>

<script>
import {getCurrentInstance, onMounted, reactive, ref} from "vue";
import AES from "@/utils/AESUtil";
import {useRouter} from "vue-router/dist/vue-router";
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
export default {
  name: "selectExamPart",
  setup(){
    const {proxy} = getCurrentInstance()
    const router = useRouter()
    // eslint-disable-next-line no-undef
    let role = ref(AES.Decrypt($cookies.get("role")))
    let loading = ref(false) //加载标识符
    let examInfo = reactive({
      arr: [],
      paper: [],
    })
    let pagination = reactive({
      pageNum: 1, //当前页
      total: Number(''), //记录条数
      pageSize: 3 //每页条数
    })

    onMounted(()=>{
      getExamInfo()
    })
    let formatterPaper = (row, column,cellValue) => {
      //row每行的内容 column每一列的属性信息 cellValue单元格的值
      if(cellValue){ //不包含值为 0 的情况 另做判断
        let paper = examInfo.paper.filter((item)=>{
          return item.id === cellValue
        })
        return cellValue + ': ' + paper[0].paperName
      }else{
        return '--' //没有值时展示 --
      }
    }
    function getExamInfo(){
      loading.value = true
      proxy.$axios.get(
          `/manage/paper`
      ).then(
          response => {
            examInfo.paper = response.data
          },
          error => {
            console.log('请求失败', error.message)
          }
      )
      if (role.value === '1'){
        proxy.$axios.get(
            // eslint-disable-next-line no-undef
            `/manage/exams/${$cookies.get("id")}/${pagination.pageNum}/${pagination.pageSize}`
        ).then(
            response => {
              examInfo.arr = [...response.data.list]
              pagination.total = response.data.total
              pagination.pageNum = response.data.pageNum
              pagination.pageSize = response.data.pageSize
              loading.value = false
            },
            error => {
              console.log('请求失败', error.message)
            }
        )
      }else if (role.value === '0'){
        proxy.$axios.get(
            // eslint-disable-next-line no-undef
            `/manage/exams/${pagination.pageNum}/${pagination.pageSize}`
        ).then(
            response => {
              examInfo.arr = [...response.data.list]
              pagination.total = response.data.total
              pagination.pageNum = response.data.pageNum
              pagination.pageSize = response.data.pageSize
              loading.value = false
            },
            error => {
              console.log('请求失败', error.message)
            }
        )
      }
    }
    function pageSizeChange(value){//改变当前记录条数
      pagination.pageSize = value
      getExamInfo()
    }
    function pageNumChange(value){//改变当前页码
      pagination.pageNum = value
      getExamInfo()
    }
    function part(id){
      router.push({ path: "/scorePart", query: { id: id } })
    }
    return{
      examInfo,
      pagination,
      loading,
      pageSizeChange,
      pageNumChange,
      formatterPaper,
      part,
      zhCn
    }
  }
}
</script>

<style scoped>
.exam {
  padding: 0px 40px;
}
.exam .page {
  margin-top: 20px;
  display: flex;
  justify-content: center;
  align-items: center;
}
.exam .edit{
  margin-left: 20px;
}
</style>